package com.dreammakerteam.frameworkdemo.qzhj.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dreammakerteam.frameworkdemo.base.domain.DAreaDo;
import com.dreammakerteam.frameworkdemo.base.domain.DBaseException;
import com.dreammakerteam.frameworkdemo.base.service.DAreaService;
import com.dreammakerteam.frameworkdemo.base.service.impl.DBaseServiceImpl;
import com.dreammakerteam.frameworkdemo.base.utils.FileUtil;
import com.dreammakerteam.frameworkdemo.base.utils.ResourceUtil;
import com.dreammakerteam.frameworkdemo.qzhj.domain.DQSearchVo;
import com.dreammakerteam.frameworkdemo.qzhj.domain.DQzhjProductAttachmentDo;
import com.dreammakerteam.frameworkdemo.qzhj.domain.DQzhjProductDo;
import com.dreammakerteam.frameworkdemo.qzhj.domain.DQzhjProductHistoryDo;
import com.dreammakerteam.frameworkdemo.qzhj.mapper.DQzhjProductMapper;
import com.dreammakerteam.frameworkdemo.qzhj.service.DQProductService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * Created by ty850 on 2017/10/11.
 */
@Service
public class DQProductServiceImpl extends DBaseServiceImpl implements DQProductService {

    @Resource
    private DQzhjProductMapper dQzhjProductMapper;

    @Resource
    private SolrClient solrClient;
    @Resource
    private DAreaService dAreaService;
    @Value("${qzhj.filePath}")
    private String filePath;

    /**
     * 增加产品
     * @param prod 产品信息
     */
    @Override
    public void add(DQzhjProductDo prod, File[] files) throws IOException, SolrServerException {

        // html转义
        prod.setProdName(HtmlUtils.htmlEscape(prod.getProdName()));
        prod.setDetailed(HtmlUtils.htmlEscape(prod.getDetailed()));
        prod.setUrl(HtmlUtils.htmlEscape(prod.getUrl()));
        prod.setEmail(HtmlUtils.htmlEscape(prod.getEmail()));
        prod.setPhone(HtmlUtils.htmlEscape(prod.getPhone()));
        prod.setName(HtmlUtils.htmlEscape(prod.getName()));

        Date now = new Date();
        // 存产品
        prod.setCreateTime(now);
        prod.insert();



        // 存附件
        DQzhjProductAttachmentDo temp;

        // 创建文件夹
        File path;
        path = new File(ResourceUtil.PROJECTPATH + filePath + prod.getId());
        if (!path.exists() && !path.mkdirs())
            throw new DBaseException("无法创建文件夹");

        String pathStr = filePath + prod.getId() + "/";

        for (File file: files) {
            if (file == null)
                continue;
            // 复制文件到自己的地方
            FileCopyUtils.copy(file, new File(path.getPath()+ "/" + file.getName()));
            if (!file.delete())
                throw new DBaseException("临时文件删除失败");
            temp = new DQzhjProductAttachmentDo();
            temp.setProdId(prod.getId());
            temp.setPath(pathStr + file.getName());

            temp.insert();
        }

        // 添加一条追踪信息
//        DQzhjProductHistoryDo history = new DQzhjProductHistoryDo();
//        history.setProdId(prod.getId());
//        history.setContent("提交产品信息");
//        history.setCreateTime(now);
//        history.insert();




        // 同步到搜索引擎

        DQSearchVo searchVo = new DQSearchVo();
        searchVo.setId(String.valueOf(prod.getId()));
        searchVo.setTitle(prod.getProdName());
        searchVo.setContent(prod.getDetailed());
        searchVo.setSys("QZHJ");
        searchVo.setType(prod.getTypeId().toString());
        searchVo.setTime(now.getTime());

        // 获取地区
        StringBuilder area = new StringBuilder();
        Long provinceId = prod.getProvinceId();
        if (provinceId != null) {
            DAreaDo dAreaDo = dAreaService.getMapper().selectById(provinceId);
            area.append(dAreaDo.getDaName());
        }
        Long cityId = prod.getCityId();
        if (cityId != null) {
            DAreaDo dAreaDo = dAreaService.getMapper().selectById(cityId);
            area.append(' ').append(dAreaDo.getDaName());
        }
        Long areaId = prod.getAreaId();
        if (areaId != null) {
            DAreaDo dAreaDo = dAreaService.getMapper().selectById(areaId);
            area.append(' ').append(dAreaDo.getDaName());
        }
        searchVo.setArea(area.toString());
        solrClient.addBean(searchVo);
        solrClient.commit();
    }

    /**
     * 列表展示
     * @return 产品信息
     */
    @Override
    public Page<DQzhjProductDo> list(int pageNum, int pageSize) {
        Page<DQzhjProductDo> page = new Page<>(pageNum, pageSize);
        page.setRecords(dQzhjProductMapper.selectPage(page, new EntityWrapper<>()));
        return page;
    }

    @Override
    public DQzhjProductMapper getMapper() {
        return dQzhjProductMapper;
    }
}
